App({
  onLaunch: function (options) {
    let that = this;

    console.log(options)
    // 登录
    wx.login({
      success: (res) => {
        let code = res.code;
        wx.request({
          url: that.globalData.host + 'login/login',
          data: { code },
          success: (res) => {
            // 存token
            wx.setStorageSync('token', res.data.data.token);
            // 存session_key
            wx.setStorageSync('session_key', res.data.data.session_key);
            // 存open_id
            wx.setStorageSync('open_id', res.data.data.open_id);
            // 存user_id
            wx.setStorageSync('user_id', res.data.data.user_id || '0');
            // 存city_id
            wx.setStorageSync('city_id', res.data.data.city.id || '0');
            // 存city
            wx.setStorageSync('city_name', res.data.data.city.title || '');
            // 存错误图片
            wx.setStorageSync('error_img', res.data.data.error_img || '');
            // 存电话
            wx.setStorageSync('phone', res.data.data.phone || '');
          }
        })
      }
    })
  },
  globalData: {
    host: 'https://wlb.sfzqg.com/api/',
    
  },

  // 确认请求时已取到user_id
  confirmLaunch(_else){
    let that = this;
    let user_id = wx.getStorageSync('user_id');
    wx.showLoading({
      title: '加载中...',
    })
    if(user_id){
      wx.hideLoading();
      _else()
    }else{
      setTimeout(function(){
        that.confirmLaunch(_else)
      },200)
    }
  },
  // 存错formid
  getFormid: function (e) {
    let that = this;
    let form_id = e.detail.formId;
    let user_id = wx.getStorageSync('user_id');
    let token = wx.getStorageSync('token');
    that.request('common/saveformid', {
      form_id, user_id, token
    }, 'POST', false, function (res) {

    })
  },
  // 请求封装
  request: function (url, params, method, check_login, _success, _fail,_complete) {
    let that = this;
    // 请求时是否要验证登录
    if (check_login == true) {
      let isSetting = wx.getStorageSync('isSetting') || false;
      let getPhone = wx.getStorageSync('getPhone') || false;
      if (!isSetting) {
        wx.showModal({
          content: '请先登录',
          success: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/account/login/login',
              })
            }
          }
        })
      } else if (!getPhone){
        wx.showModal({
          content: '请先授权手机号',
          success: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/account/phone/phone',
              })
            }
          }
        })
      } else {
        that.getData(url, params, method, _success, _fail,_complete);
      }
    } else {
      that.getData(url, params, method, _success, _fail,_complete);
    }
  },
  // 请求数据
  getData: function (url, params, method, _success, _fail,_complete) {
    let that = this;
    params['token'] = wx.getStorageSync('token');
    params['user_id'] = wx.getStorageSync('user_id');
    // wx.showLoading({
    //   title: '加载中...',
    // })
    wx.request({
      url: that.globalData.host + url,
      data: params,
      method: method == 'POST' ? 'POST' : 'GET',
      header: {
        "Content-type": method == 'POST' ? "application/x-www-form-urlencoded" : "application/json",
      },
      success: (res) => {
        wx.hideLoading();
        if(res.data.code == 0){
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }else{
          _success && _success(res)
        }
      },
      fail: (res) => {
        wx.hideLoading();
        _fail && _fail(res)
      },
      complete: (res) => {
        _complete && _complete()
      }
    })
  },
  upload: function (_url, _filePath, _formData, _success, _fail) {
    let that = this;
    wx.uploadFile({
      url: that.globalData.host + _url,
      filePath: _filePath,
      name: 'file',
      formData: _formData,
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: (res) => {
        _success(res)
      },
      fail: (res) => {
        _fail(res)
      }
    })
  },
  // 点击事件的验证登录
  confirmTap: function (_else) {
    let that = this;
    let isSetting = wx.getStorageSync('isSetting');
    let getPhone = wx.getStorageSync('getPhone');
    if (!isSetting) {
      wx.showModal({
        content: '请先登录',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/account/welcome/welcome',
            })
          }
        }
      })
    } else if (!getPhone){
      wx.showModal({
        content: '请先授权手机号',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/account/phone/phone',
            })
          }
        }
      })
    } else {
      _else();
    }
  },
  // 跳转事件
  skipPage:function(e){
    let that = this;
    let open = e.currentTarget.dataset.open;
    let url = e.currentTarget.dataset.url || '';
    if (open == 'navigate'){
      wx.navigateTo({
        url: url,
      })
    } else if (open == 'redirect'){
      wx.redirectTo({
        url: url,
      })
    } else if (open == 'switchTab'){
      wx.switchTab({
        url: url,
      })
    } else if (open == 'navigateBack'){
      wx.navigateBack({
        delta: 1,
      })
    }
  }
})